package com.geek.game.manager.agent.service.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.geek.game.manager.agent.model.ConsumerRecord;
import com.geek.game.manager.agent.model.Manager;
import com.geek.game.manager.agent.service.ConsumerService;

import ydh.cicada.dao.JdbcDao;
import ydh.cicada.dao.Page;
import ydh.cicada.query.QueryObject;

@Service
@Transactional(rollbackFor={Exception.class})
public class ConsumerServiceImpl implements ConsumerService{

	@Autowired
	private JdbcDao dao;

	public int insertRecord(ConsumerRecord record) {
		System.err.println(1);
		dao.insert(record);
		return 1;
	}

	public void setDao(JdbcDao dao) {
		this.dao = dao;
	}

	public Page<Manager> getManagerTable(Map map) {
		QueryObject<Manager> query=QueryObject.select("t.user_id id,t.user_account userAccount,t.user_name userName,t.user_type userType,t.`password`,"+
        " t.create_time createTime  ,t.update_time updateTime,t.agent_coin agentCoin,t.player_id playerId,"+
        " t.parent_manager parentManager,t.admin_desc adminDesc,"+
        " (select GROUP_CONCAT(club_name) from club where club_manager_id=t.user_id) clubStr,"+
        " (select GROUP_CONCAT(club_Id) from club where club_manager_id=t.user_id) clubIdStr,"+
        " (select SUM(r.mahjong_coin) from t_recharge_record r where r.user_id=t.user_id and TO_DAYS(r.record_time)=TO_DAYS(NOW())) todaySale,"+
		" (select SUM(r.mahjong_coin) from t_recharge_record r where r.player_id=t.user_id) totalBuy",Manager.class)
				.from("t_manager t")
				.cond("t.deltag").notEqu(1)
				.page(Long.valueOf(map.get("pageNumber").toString()))
				.pageSize(Integer.valueOf(map.get("pageSize").toString()));
		if(map.get("sortName")!=null && map.get("sortOrder")!=null){
			if(map.get("sortOrder").toString().equals("asc")){
				query.asc(map.get("sortName").toString());
			}else{
				query.desc(map.get("sortName").toString());
			}
		}
		if(map.get("userType")!=null){
			query.cond("t.user_type").equ(map.get("userType"));
		}
		if(map.get("searchText")!=null){
			String searchKey="%"+map.get("searchText").toString()+"%";
			query.condition("t.user_id like ? or t.admin_desc like ? or t.user_name like ? or t.user_account like ?", new Object[]{searchKey,searchKey,searchKey,searchKey});
		}
		query.desc("update_time")
		.desc("clubStr");
		Page<Manager> page=query.find(dao);
		map.put("rows", page.getData());
		map.put("total",page.getTotalSize());
		return page;
	}
	
	
}
